import 'package:fl_chart/fl_chart.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';

class ChartView extends StatelessWidget {
  final Color color;

  ChartView({super.key, required this.color});

  var list = <double>[
    100.0,
    170.0,
    130.0,
    190.0,
    150.0,
    120.0,
    100.0,
    170.0,
    130.0,
    190.0,
    150.0,
    120.0,
    100.0,
    170.0,
    130.0,
    190.0,
    150.0,
    120.0,
    100.0,
    170.0,
    130.0,
    190.0,
    150.0,
    120.0,
    100.0,
    170.0,
    130.0,
    190.0,
    150.0,
    120.0,
    111,
  ];

  @override
  Widget build(BuildContext context) {
    return Container(
      margin: const EdgeInsets.only(right: 0),
      height: 180,
      child:
          // Obx(
          //   () =>
          BarChart(
        BarChartData(
          // maxY: 200,
          gridData: const FlGridData(show: false),
          borderData: FlBorderData(
            show: true,
            border: const Border(
              bottom: BorderSide(color: Colors.grey), // 底部边线的样式
              left: BorderSide(color: Colors.grey), // 左侧边线的样式
              right: BorderSide(color: Colors.transparent), // 右侧边线的样式
              top: BorderSide(color: Colors.transparent), // 顶部边线的样式，这里设置为透明来隐藏
            ),
          ),
          barGroups: _getBarData(list),
          titlesData: FlTitlesData(
            show: true,
            topTitles: const AxisTitles(
              sideTitles: SideTitles(showTitles: false),
            ),
            rightTitles: const AxisTitles(
              sideTitles: SideTitles(showTitles: false),
            ),
            bottomTitles: AxisTitles(
              sideTitles: SideTitles(
                showTitles: true,
                reservedSize: 30,
                getTitlesWidget: getTitles,
              ),
            ),
            // bottomTitles: AxisTitles(
            //   sideTitles: _bottomTitles,
            // ),
          ),
        ),
      ),
      // ),
    );
  }

  Widget getTitles(double value, TitleMeta meta) {
    const style = TextStyle(
      // fontWeight: FontWeight.bold,
      fontSize: 12,
    );
    String text;
    int length = 31; //根据月份推算是多少天
    switch (value.toInt()) {
      case 0:
      case 5:
      case 10:
      case 15:
      case 20:
      case 25:
        text = '${value.toInt() + 1}';
      default:
        if (value.toInt() == length - 1) {
          text = '${value.toInt() + 1}';
        } else {
          text = '';
        }
        break;
    }

    return SideTitleWidget(
      axisSide: meta.axisSide,
      space: 4,
      child: Text(text, style: style),
    );
  }

  List<BarChartGroupData> _getBarData(List<double> list) {
    List<BarChartGroupData> resultList = [];
    for (int i = 0; i < list.length; i++) {
      var res = BarChartGroupData(x: i, barRods: [
        BarChartRodData(
          toY: list[i],
          color: color,
          // borderRadius: BorderRadius.zero,
        )
      ]);
      resultList.add(res);
    }
    return resultList;
  }
}
